from get_service_data import get_services
from get_instance_data import get_instances
import pandas as pd
import math
import datetime
import base64
import json
from elasticsearch import Elasticsearch
from es_config import ES_IP, get_now_date
es = Elasticsearch(ES_IP)
instance_df_head = ['service_instance_cpm', 'service_instance_sla',
                    'service_instance_resp_time', 'instance_jvm_cpu', 'instance_jvm_memory_heap',
                    'instance_jvm_memory_noheap']

print(es)
def serviceName_to_instances(serviceName, time1):
    _, mapping = get_service_instance_dict(time1)
    # mapping = json.loads(mapping)
    instances = mapping[serviceName]
    return instances


def choice_body(date, tag):
    date = date.strftime("%Y%m%d%H%M")
    if tag == 'start':
        body = {
            'query': {
                'range': {
                    'time_bucket':
                        {
                            'gt': date
                        }
                }
            }
        }
    elif tag == 'end':
        body = {
            'query': {
                'range': {
                    'time_bucket':
                        {
                            'lte': date
                        }
                }
            }
        }
    else:
        tag = tag.strftime("%Y%m%d%H%M")
        body = {
            'query': {
                'range': {
                    'time_bucket':
                        {
                            'gt': date,
                            'lte': tag
                        }
                }
            }
        }
    return body


def get_instance_data(mapping, index, date, tag):
    d = date.strftime("%Y%m%d")
    file_name = instance_df_head[index] + '-' + d
    body = choice_body(date, tag)
    allDoc = es.search(index=file_name, body=body, scroll='5m', size=100)
    results = allDoc['hits']['hits']  # 第一页
    total = allDoc['hits']['total']['value']
    scroll_id = allDoc['_scroll_id']
    for i in range(0, int(total / 100) + 1):
        query_scroll = es.scroll(scroll_id=scroll_id, scroll='5m')['hits']['hits']
        results += query_scroll    #结果全部加载到内存
    for result in results:
        entity_id = result['_source']['entity_id'].split('_')
        time_bucket = str(result['_source']['time_bucket'])
        if len(time_bucket) != 12:
            pass
        # print(entity_id)
        service_id, instance_id = 0,0
        try:
            service_id = str(base64.b64decode(entity_id[0].encode('utf-8')), encoding='utf-8')
            instance_id = str(base64.b64decode(entity_id[1].encode('utf-8')), encoding='utf-8')
        except:
            service_id = str(base64.b64decode(entity_id[0][-2].encode('utf-8')), encoding='utf-8')
            instance_id = str(base64.b64decode(entity_id[1].encode('utf-8')), encoding='utf-8')
        if service_id not in mapping:
            mapping[service_id] = []
        if instance_id not in mapping[service_id]:
            mapping[service_id].append(instance_id)
    return mapping


def get_service_instance_dict(time1):
    # now_time = datetime.datetime.now() + datetime.timedelta(hours=8)
    now_time = time1
    start_time = now_time - datetime.timedelta(hours=0.5)
    mapping = {}
    for index in range(len(instance_df_head)):
        if now_time.strftime("%Y%m%d") == start_time.strftime("%Y%m%d"):
            mapping = get_instance_data(mapping, index, start_time, now_time)
        else:
            mapping = get_instance_data(mapping, index, start_time, 'start')
            mapping = get_instance_data(mapping, index, now_time, 'end')
    services = set(mapping.keys())
    # mapping = json.dumps(mapping, ensure_ascii=False)
    return services, mapping


def get_instance_minutes(num, time1):     #instance
    hour = math.ceil(num / 60)
    df = get_instances(hour, time1)
    rows = list(range(hour * 60 - num, hour * 60))
    latest = df.loc[rows, :]
    latest.to_csv('./data/instance_info.csv', header=True, index=True)
    return latest


def get_service_minutes(keys, num, time1):  #service
    hour = math.ceil(num / 60)
    df, _ = get_services(hour, time1)
    diff = keys.difference(_)
    rows = list(range(hour * 60-num, hour * 60))
    latest = df.loc[rows, :]
    for d in iter(diff):
        for i in range(num):
            zero = {'service_id': [d], 'service_apdex': [0], 'service_resp_time': [0], 'service_sla': [0], 'service_cpm': [0]}
            zero = pd.DataFrame(zero, index=[hour*60-num+i])
            latest = pd.concat([latest, zero])
    latest.to_csv('./data/service_info.csv', header=True, index=True)
    return latest


if __name__ == '__main__':
    # minutes = 20   #向前获取多少分钟
    # df, keys = get_services(1)
    # get_service_minutes(keys, minutes)
    # get_instance_minutes(minutes)
    # services, _ = get_service_instance_dict()   #获取service名
    print("")
#     serviceName = 'test1'
#     instances = serviceName_to_instances(serviceName)   #根据serviceName获取对应的instance
#     print(services)
#     print(instances)